package ic.ast.decl;

import common.exceptions.SemanticError;
import ic.ast.Visitor;
import ic.types.TypeTable;

/**
 * User-defined data type AST node.
 * 
 */
public class ClassType extends Type {

	private String name;

	public Object accept(Visitor visitor) throws Exception {
		return visitor.visit(this);
	}

	/**
	 * Constructs a new user-defined data type node.
	 * 
	 * @param line
	 *            Line number of type declaration.
	 * @param name
	 *            Name of data type.
	 */
	public ClassType(int line, String name) {
		super(line);
		this.name = name;
	}

	public String getClassName() {
		return name;
	}
	
	public String getDisplayName() {
		return name;
	}

    @Override
    public String getOutputName() {
        return name;
    }

    @Override
    public Type clone(int newDimension) {
        Type newType = new ClassType(getLine(), name);
        for(int i = 0; i < newDimension; i++) {
            newType.incrementDimension();
        }
        return newType;
    }

    @Override
    public boolean subTypeOf(Type otherType) {
        boolean returnVal = false;
        if (this.getArrayDimension() > 0 || otherType.getArrayDimension() > 0) {
            return this == otherType;
        }
        if (otherType instanceof ClassType) {
            try {
                returnVal = TypeTable.getClassTypeByName(this.getClassName()).subTypeOf(TypeTable.getClassTypeByName(((ClassType) otherType).getClassName()));
            } catch (SemanticError e) {     }
        }
        return returnVal;
    }

    @Override
    public boolean equals(Object other) {
        return (other instanceof ClassType) &&
                this.name.equals(((ClassType) other).name) &&
                this.getArrayDimension() == ((ClassType)other).getArrayDimension();
    }

}
